#!/usr/bin/env python
# -*- coding: utf-8 -*-
# models/stock_picking.py   
from odoo import models, fields, api
 
class StockPickingType(models.Model):  
    _inherit = 'stock.picking.type'   

    code = fields.Selection(selection_add=[
        ('expired_quality', 'Expired Quality')
    ], ondelete={'expired_quality': lambda recs: recs.write({'code': 'internal', 'active': False})})

    @api.depends('code')
    def _compute_show_picking_type(self):
        super()._compute_show_picking_type()
        for record in self:
            if record.code == 'expired_quality':
                record.show_picking_type = True

#  from odoo import models, fields, api
 
class StockPicking(models.Model):
    _inherit = 'stock.picking'

    def action_open_quality_check_picking(self):
        if self.picking_type_id.code == 'expired_quality':
            action_pool = self.env["ir.actions.actions"]
            action = action_pool._for_xml_id("sb_product_expiry_ext.action_quality_check_view_qc")
            action['context'] = self.env.context.copy()
            action['domain'] = [('team_id','=',self.env.ref('sb_product_expiry_ext.quality_alert_qc_team0').id)]
            action['context'].update({
                'search_default_picking_id': [self.id],
                'default_picking_id': self.id,
                'show_lots_text': self.show_lots_text,
            })
            return action
        else:
            return super(StockPicking, self).action_open_quality_check_picking()
#
    #  扩展code字段的可用选项（可选：仅用于校验）
    #  def _selection_picking_type_code(self):
        #  codes = super()._selection_picking_type_code()
        #  codes.append(('expired_quality',  '过期质检'))
        #  return codes
 
    # 添加质检触发逻辑  
    #def button_validate(self):  
    #    if self.picking_type_id.code  == 'expired_quality':  
    #        self._trigger_expired_quality_checks()  
    #    return super().button_validate()  
 
    #def _trigger_expired_quality_checks(self):  
    #    """ 为过期质检类型生成质检任务 """  
    #    QualityCheck = self.env['quality.check']   
    #    for move in self.move_ids:   
    #        check = QualityCheck.create({   
    #            'picking_id': self.id,   
    #            'product_id': move.product_id.id,   
    #            'point_id': self.env.ref('sb_product_expiry_ext.quality_check_expired').id,   
    #            'qty_to_test': move.product_uom_qty   
    #        })  
    #        check.action_approve()   # 或保持待处理状态  
